/*
 * @Author: xiaosihan
 * @Date: 2024-09-25 17:11:46
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-27 09:59:55
 */

import uniq from 'lodash/uniq';
import { Material, Mesh, Object3D } from 'three';

// 递归遍历材质对象
export default function extractMaterial(object: Object3D) {
  const materials: Array<Material> = [];
  object.traverse((o) => {
    const mesh = o as Mesh;
    if (mesh.isMesh) {
      const material = mesh.material;
      if (material instanceof Array) {
        materials.push(...material);
      } else {
        materials.push(material);
      }
    }
  });
  return uniq(materials);
}
